NVM.NovemSlider = (function () {
  var __core = NOVEM.Core;
  var __base = NOVEM.Base;
  var __modules = NOVEM.Modules;

  return {
    init: function (selector) {
      if (__core.getSelector(selector, false, false).length < 1) {
        return true;
      }

      __core.loadJS({
        file: "plugins/swiper.js",
        id: "nvm-swiper-js",
        jsFolder: true,
      });

      __core
        .isFuncTrue(function () {
          return typeof Swiper !== "undefined";
        })
        .then(function (cond) {
          if (!cond) {
            return false;
          }

          __core.initFunction({
            class: "has-plugin-swiper",
            event: "pluginSwiperReady",
          });

          selector = __core.getSelector(selector, false);
          if (selector.length < 1) {
            return true;
          }

          selector.forEach(function (element) {
            if (!element.classList.contains("slider-element")) {
              return true;
            }

            if (element.querySelectorAll(".swiper-slide").length < 1) {
              return true;
            }

            let userOptions, pagerOptions;
            if (element.dataset.swiperOptions != undefined) {
              userOptions = JSON.parse(
                element.dataset.swiperOptions.replace(/'/g, '"')
              );

              // Pager
              if (userOptions.pager) {
                pagerOptions = {
                  pagination: {
                    el: ".swiper-pagination",
                    clickable: true,
                    bulletActiveClass: "swiper-page-item-active",
                    bulletClass: "swiper-page-item",
                    renderBullet: function (index, className) {
                      return (
                        '<span class="' +
                        className +
                        '">' +
                        (index + 1) +
                        "</span>"
                      );
                    },
                  },
                };
              }
            }




            // Slider init
            let options = { ...userOptions, ...pagerOptions };
            let nvmswiper = new Swiper(
              element.querySelector(".swiper"),
              options
            );

            // Tabs (linked content)
            if (userOptions.tabs) {
              nvmswiper.on("activeIndexChange", (e) => {
                let targetTab = document.querySelector(
                  e.slides[e.activeIndex].dataset.swiperTab
                ),
                  previousTab = document.querySelector(
                    e.slides[e.previousIndex].dataset.swiperTab
                  );

                previousTab.classList.remove("active");
                targetTab.classList.add("active");
              });
            }

            if (userOptions.thumbs) {
              if (!nvmswiper.thumbs || nvmswiper.thumbs.destroyed) { return true; };
              document.querySelectorAll(userOptions.thumbs.swiper + " .swiper-wrapper .swiper-slide").forEach(function (ele) {
                ele.addEventListener("mouseover", (event) => {
                  if (ele.classList.contains(nvmswiper.params.thumbs.slideThumbActiveClass)) { return true; }
                  let slideToIndex;
                  slideToIndex = parseInt(
                    ele.getAttribute('data-swiper-slide-index'),
                    10,
                  );
                  if (nvmswiper.params.loop) {
                    nvmswiper.slideToLoop(slideToIndex);
                  } else {
                    nvmswiper.slideTo(slideToIndex);
                  }

                }, false);
              });
            }

            // if (nvmswiper.el.classList.contains('swiper-thumbs')) {
            //   console.log(nvmswiper.slides);
            //   nvmswiper.slides.forEach(function (element) {

            //     element.addEventListener(
            //       "mouseover",
            //       (event) => {
            //         console.log(element.dataset.swiperSlideIndex);
            //         nvmswiper.slides[element.dataset.swiperSlideIndex].classList.add('swiper-slide-thumb-active');
            //       },
            //       false,
            //     );
            //   });
            // }
            //   element.querySelector(".slider-element"),
            //   ""
            // );
          });

        });
    },
  };
})();